﻿using dotnet2cache.Abstractions;
using dotnet2cache.Abstractions.Bus;
using dotnet2cache.Abstractions.Serialization;
using dotnet2cache.Core;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace dotnet2cache.CsRedis
{
    public static class DTCsRedisExtensions
    {


        public static CacheProvider AddDTLevel2CacheProvider(this CacheProvider provider, RedisOption redisOption)
        {
            
            if(redisOption==null||string.IsNullOrEmpty(redisOption.ConnectionString))
            {
                throw new DtArgumentNull("redisOption");
            }

            var csRedisClient = new CSRedis.CSRedisClient(redisOption.ConnectionString);

            L2CacheRedisHelper.Initialization(csRedisClient);

            provider.Services.AddSingleton<ILevel2CacheProvider, Level2CacheProvider>();

            return provider;
        }



        

        public static CacheProvider AddDTCsRedisBusProvider(this CacheProvider provider, RedisOption redisOption)
        {
            if (redisOption == null || string.IsNullOrEmpty(redisOption.ConnectionString))
            {
                throw new DtArgumentNull("redisOption");
            }

            var csRedisClient = new CSRedis.CSRedisClient(redisOption.ConnectionString);

            BusRedisHelper.Initialization(csRedisClient);

           provider.Services.AddSingleton<IMessageBus, RedisBus>();
            provider.Services.AddSingleton<IDataSerializer, DataSerializer>();

            return provider;
        }

    }
}
